﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Confucius.entity;
using System.IO.IsolatedStorage;
using Confucius.utils;
using Confucius.daoImpl;

namespace Confucius.pages
{
    public partial class FinishedQuestsPage : PhoneApplicationPage
    {
        List<Quest> finishedQuestList;
        String currentGameID;

        public FinishedQuestsPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(FinishedPage_Loaded);
        }

        private void FinishedPage_Loaded(object sender, RoutedEventArgs e)
        {
            currentGameID = "";
            if (IsolatedStorageSettings.ApplicationSettings.Contains(Constants.CURRENT_GAMEID))
            {
                currentGameID = (string)IsolatedStorageSettings.ApplicationSettings[Constants.CURRENT_GAMEID];
            }
            if (currentGameID == "")
            {
                MessageBox.Show("Cannot find matched quests, please contact team Confucius.");
                NavigationService.GoBack();
            }

            finishedQuestList = new QuestDAOImpl().retreiveQuests(currentGameID);
            if (ConfuciusUtil.finishedQuestsCount(finishedQuestList) > 0)
            {
                Notification.Visibility = Visibility.Collapsed;
                FinishedListbox.ItemsSource = finishedQuestList;
            }
            else
            {
                Notification.Visibility = Visibility.Visible;
            }
        }

        private void FinishedListbox_Tap(object sender, GestureEventArgs e)
        {
            Quest data = ((FrameworkElement)e.OriginalSource).DataContext as Quest;
            NavigationService.Navigate(new Uri("/pages/ObjectDetailPage.xaml?questID=" + data.QuestID,
                UriKind.Relative));
        }
    }
}